package com.ktg.web.mainData;

import com.ktg.ktgcommon.domain.mainData.MdItemType;
import com.ktg.ktgcommon.result.R;
import com.ktg.service.MDItemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api")
public class ProductTypeController {

    @Autowired
    private MDItemTypeService mdItemTypeService;

    //直接查询全部数据
    @PostMapping("/MdItemType/CheckMdItemTypeAll")
    public R CheckMdItemTypeAll() {
        List<MdItemType> list = mdItemTypeService.CheckMdItemTypeAll();
        return R.OK(list);
    }
    //条件查询
    @PostMapping("/MdItemType/checkMdItemTypeCondition")
    public R checkMdItemTypeCondition(String enableFlag,String itemTypeName) {
        List<MdItemType> mdItemType = mdItemTypeService.checkMdItemTypeCondition(enableFlag,itemTypeName);
        return R.OK(mdItemType);
    }

    //根据id删除特定数据
    @DeleteMapping("/MdItemType/delProduct/{itemTypeId}")
    public R delProduct(@PathVariable("itemTypeId")Integer itemTypeId) {
        Integer integer= mdItemTypeService.delProduct(itemTypeId);
        return R.OK(integer);
    }
    //查询单条的数据
    @PostMapping("/MdItemType/checkProductById/{itemTypeId}")
    public R checkProductById(@PathVariable("itemTypeId")Integer itemTypeId) {
        //查询单条的数据
        MdItemType mdItemType= mdItemTypeService.checkProductById(itemTypeId);
        //查询单条数据的父数据,但是原始的数据查询不到所以应该排除
        if (mdItemType.getParentTypeId()!=0){
            MdItemType mdItemTypeParent= mdItemTypeService.checkProductByParentId(mdItemType.getParentTypeId());
            mdItemType.setParentTypeName(mdItemTypeParent.getItemTypeName());
        }
        return R.OK(mdItemType);
    }
    //新增数据
    @PostMapping("/MdItemType/addProduct")
    public R addProduct(MdItemType mdItemType) {
        System.out.println("=========");
        System.out.println(mdItemType);
        System.out.println("=========");
        Integer integer=mdItemTypeService.addProduct(mdItemType);
        return R.OK(integer);
    }

    //修改数据
    @PostMapping("/MdItemType/changProduct")
    public R changProduct(MdItemType mdItemType) {
        Integer integer=mdItemTypeService.changProduct(mdItemType);
        return R.OK(integer);
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // 禁止绑定 children 字段
        binder.setDisallowedFields("children");
    }
}
